package com.boss.salary.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IAgencyInfoDao;
import com.boss.salary.entity.AgencyExt;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.service.IAgencyExtService;
import com.boss.salary.service.IAgencyInfoService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AgencyInfoServiceImpl implements IAgencyInfoService {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(AgencyInfoServiceImpl.class);

    @Autowired
    private IAgencyInfoDao agencyInfoDao;

    @Autowired
    private IAgencyExtService agencyExtService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAgencyInfo(AgencyInfo agencyInfo) {
//        String agencyId = SysUtils.generateGuid();
        String agencyId = agencyInfo.getId();
        // 保存单位表
        agencyInfo.setId(agencyId);
        agencyInfo.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        agencyInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        agencyInfo.setIsDeleted(SystemDictConstant.IS_FALSE);
        agencyInfoDao.insertAgencyInfo(agencyInfo);
        // 保存单位信息拓展表
        AgencyExt agencyExt = new AgencyExt();
        agencyExt.setAgencyId(agencyId);
        agencyExt.setAgencyAddress(agencyInfo.getAgencyAddress());
        agencyExt.setZip(agencyInfo.getZip());
        agencyExt.setAgencyAdmLevelCode(agencyInfo.getAgencyAdmLevelCode());
        agencyExt.setAdmDepTypeCode(agencyInfo.getAdmDepTypeCode());
        if (!StringUtils.isEmpty(agencyInfo.getAgencyBankNames())) {
            agencyExt.setAgencyBankName(agencyInfo.getAgencyBankNames().toString().replaceAll(" ", ""));
        }
        agencyExt.setAgencyBankCardno(agencyInfo.getAgencyBankCardno());
        agencyExt.setCentralAgencyCode(agencyInfo.getCentralAgencyCode());
        agencyExt.setAgencyLeader(agencyInfo.getAgencyLeader());
        agencyExt.setIsUniSala(agencyInfo.getIsUniSala());
        agencyExt.setRemarks(agencyInfo.getRemarks());
        agencyExt.setHasFile(agencyInfo.getHasFile());
        agencyExt.setFundSupCode(agencyInfo.getFundSupCode());
        agencyExt.setCapitalType(agencyInfo.getCapitalType());
        agencyExt.setEffectiveYear(agencyInfo.getEffectiveYear());
        agencyExt.setVerifyState(agencyInfo.getVerifyState());
        agencyExtService.insertAgencyExt(agencyExt);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAgencyInfo(Map<String, Object> queryParam) {
        agencyInfoDao.deleteAgencyInfo(queryParam.get("id").toString());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAgencyInfo(AgencyInfo agencyInfo) {
        // 更新单位信息
//        agencyInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
//        agencyInfoDao.updateAgencyInfo(agencyInfo);
        // 更新单位信息拓展表
        AgencyExt agencyExt = new AgencyExt();
        agencyExt.setAgencyId(agencyInfo.getId());
//        agencyExt.setAgencyAddress(agencyInfo.getAgencyAddress());
//        agencyExt.setZip(agencyInfo.getZip());
//        agencyExt.setAgencyAdmLevelCode(agencyInfo.getAgencyAdmLevelCode());
//        agencyExt.setAdmDepTypeCode(agencyInfo.getAdmDepTypeCode());
        if (!StringUtils.isEmpty(agencyInfo.getAgencyBankNames())) {
            agencyExt.setAgencyBankName(agencyInfo.getAgencyBankNames().toString().replaceAll(" ", ""));
        }
        agencyExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
//        agencyExt.setAgencyBankCardno(agencyInfo.getAgencyBankCardno());
//        agencyExt.setCentralAgencyCode(agencyInfo.getCentralAgencyCode());
//        agencyExt.setAgencyLeader(agencyInfo.getAgencyLeader());
//        agencyExt.setIsUniSala(agencyInfo.getIsUniSala());
//        agencyExt.setRemarks(agencyInfo.getRemarks());
//        agencyExt.setHasFile(agencyInfo.getHasFile());
//        agencyExt.setFundSupCode(agencyInfo.getFundSupCode());
//        agencyExt.setCapitalType(agencyInfo.getCapitalType());
//        agencyExt.setEffectiveYear(agencyInfo.getEffectiveYear());
        if (!SystemDictConstant.IS_REJECT.equals(agencyInfo.getVerifyState())) {
            agencyExt.setVerifyState(SystemDictConstant.NO_SUBMIT);
        } else {
            agencyExt.setVerifyState(SystemDictConstant.IS_REJECT);
        }
        agencyExtService.updateAgencyExtBank(agencyExt);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAgencyInfoIsPaymentAgency(AgencyInfo agencyInfo) {
        agencyInfoDao.updateAgencyInfo(agencyInfo);
        agencyInfoDao.updateAgencyExt(agencyInfo);
        agencyInfoDao.updateAgencyVerify(agencyInfo);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAgencyInfoAudit(AgencyInfo agencyInfo) {
        // 更新单位信息
//        agencyInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
//        agencyInfoDao.updateAgencyInfo(agencyInfo);
        // 更新单位信息拓展表
        AgencyExt agencyExt = new AgencyExt();
        agencyExt.setAgencyId(agencyInfo.getId());
        if (!StringUtils.isEmpty(agencyInfo.getAgencyBankNames())) {
            agencyExt.setAgencyBankName(agencyInfo.getAgencyBankNames().toString().replaceAll(" ", ""));
        }
        agencyExt.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        agencyExt.setVerifyState(SystemDictConstant.IS_AUDIT);
        agencyExtService.updateAgencyExtBank(agencyExt);
    }


    @Override
    public PageDTO<AgencyInfo> queryAgencyInfoPage(PaginationDTO paginationDTO, Map<String, Object> queryParam, List<String> childrenId) throws AppRuntimeException {
        List<AgencyInfo> agencyInfos = agencyInfoDao.queryAgencyInfoPage(paginationDTO, queryParam, childrenId);
        int total = agencyInfoDao.queryAgencyInfoTotal(queryParam, childrenId);
        PageDTO<AgencyInfo> PageDTO = new PageDTO<>();
        PageDTO.setRows(agencyInfos);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    public Map<String, Object> getAgencyNameAndState(String agencyId) {
        return agencyInfoDao.selectAgencyNameAndState(agencyId);
    }

    @Override
    public List<AgencyInfo> queryAgencyInfo(List<String> childrenId) {
        return agencyInfoDao.queryAgencyInfo(childrenId);
    }

    @Override
    public List<AgencyInfo> queryAgencyInfoById(Map<String, Object> queryParam) {
        return agencyInfoDao.queryAgencyInfoById(queryParam);
    }

    @Override
    public List<AgencyInfo> getAgencyTreeData(String agencyId,String mofDivCode) {
        return agencyInfoDao.getAgencyTreeData(agencyId,mofDivCode);
    }

    @Override
    public List<AgencyInfo> getAgencyTree(String agencyId,String mofDivCode) {
        return agencyInfoDao.getAgencyTree(agencyId,mofDivCode);
    }

    @Override
    public List<AgencyInfo> getAgencyTreeDatas(String agencyId,String mofDivCode) {
        return agencyInfoDao.getAgencyTreeDatas(agencyId,mofDivCode);
    }

    @Override
    public Map<String, Object> getFirstAuthAgencyData(String agencyId) {
        AgencyInfo agencyInfo = agencyInfoDao.selectFirstAuthAgencyData(agencyId);
        Map<String, Object> map = new HashMap<>();
        map.put("id", agencyInfo.getId());
        map.put("code", agencyInfo.getAgencyCode());
        map.put("label", agencyInfo.getAgencyCode() + "-" + agencyInfo.getAgencyName());
        map.put("isleaf", String.valueOf(agencyInfo.getIsLeaf()));
        return map;
    }

    @Override
    public List<String> getSendSalaryAgencyId(JSONArray jsonArray,UserDTO userDTO) {
        return agencyInfoDao.selectSendSalaryAgencyId(jsonArray,userDTO);
    }

    @Override
    public List<AgencyInfo> getAllAgency(UserDTO userDTO) {
        return agencyInfoDao.getAllAgency(userDTO);
    }

    @Override
    public AgencyInfo getNameById(String id) {
        return agencyInfoDao.getNameById(id);
    }

    @Override
    public List<String> getIds() {
        return agencyInfoDao.getIds();
    }

    @Override
    public List<String> getAgencyIds(List<String> agencyCodes) {
        return agencyInfoDao.getAgencyIds(agencyCodes);
    }


    @Override
    public List<AgencyInfo> getNotAuditAuthAgencyTreeData(String agencyId,String mofDivCode) {
        return agencyInfoDao.getNotAuditAuthAgencyTreeData(agencyId,mofDivCode);
    }

    @Override
    public int agencyPerson(String id) {
        return agencyInfoDao.agencyPerson(id);
    }

}
